using System;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Toad.Application.Actions
{
  public class InsertOpbSlaveIpBlockAction 
    : ActionBase
  {
    private string Format(
      string name,
      uint interrupts, 
      int datawidth, 
      uint chip_enable, 
      uint baseAddress,
      uint highAddress
    )
    {
      return
        "ipblock " + name + " (                                 \n" +
        "  in  IP2Bus_IntrEvent   : ns(" + interrupts + ");     \n" +
        "  out Bus2IP_Data        : ns(" + datawidth + ");      \n" +
        "  out Bus2IP_BE          : ns(" + (datawidth / 8) + ");\n" +
        "  out Bus2IP_RdCE        : ns(" + chip_enable + ");    \n" +
        "  out Bus2IP_WrCE        : ns(" + chip_enable + ");    \n" +
        "  in  IP2Bus_Data        : ns(" + datawidth + ");      \n" +
        "  in  IP2Bus_Ack         : ns(1);                      \n" +
        "  in  IP2Bus_Retry       : ns(1);                      \n" +
        "  in  IP2Bus_Error       : ns(1);                      \n" +
        "  in  IP2Bus_ToutSup     : ns(1)                       \n" +
        "){                                                     \n" +
        "  iptype \"opbipifslave\";                             \n" +
        "  ipparm \"registers=" + chip_enable + "\";            \n" +
        "  ipparm \"interrupts=" + interrupts + "\";            \n" +
        "  ipparm \"low_address=" + string.Format("0x{0:X}", baseAddress) + "\";          \n" +
        "  ipparm \"high_address=" + string.Format("0x{0:X}", highAddress) + "\";         \n" +
        "}                                                      \n" +
        "                                                       \n" +
        "dp my_dp(                                              \n" +
        "  out IP2Bus_IntrEvent   : ns(" + interrupts + ");     \n" +
        "  in  Bus2IP_Data        : ns(" + datawidth + ");      \n" +
        "  in  Bus2IP_BE          : ns(" + (datawidth / 8) + ");\n" +
        "  in  Bus2IP_RdCE        : ns(" + chip_enable + ");    \n" +
        "  in  Bus2IP_WrCE        : ns(" + chip_enable + ");    \n" +
        "  out IP2Bus_Data        : ns(" + datawidth + ");      \n" +
        "  out IP2Bus_Ack         : ns(1);                      \n" +
        "  out IP2Bus_Retry       : ns(1);                      \n" +
        "  out IP2Bus_Error       : ns(1);                      \n" +
        "  out IP2Bus_ToutSup     : ns(1)                       \n" +
        ")                                                      \n" +
        "{                                                      \n" +
        "  sfg run {                                            \n" +
        "   IP2Bus_IntrEvent = 0;                               \n" +
        "   IP2Bus_Data		   = 0;                               \n" +
        "   IP2Bus_Ack       = 0;                               \n" +
        "   IP2Bus_Retry     = 0;                               \n" +
        "   IP2Bus_Error     = 0;                               \n" +
        "   IP2Bus_ToutSup   = 0;                               \n" +
        "  }                                                    \n" +
        "}                                                      \n" +
        "                                                       \n" +
        "hardwired ctrl(my_dp) { run; }                         \n" +
        "                                                       \n" +
        "system s {                                             \n" +
        "  " + name + "(a, b, c, d, e, f, g, h, i, j);          \n" +
        "  my_dp(a, b, c, d, e, f, g, h, i, j);                 \n" +
        "}                                                      \n";
    }

    public InsertOpbSlaveIpBlockAction()
		{
      Enabled = false;

      Application.Instance.SelectedItemChanged +=
        new EventHandler<SelectedItemChangedEventArgs>(Instance_SelectedItemChanged);
		}

		public override void Execute(object sender, EventArgs e)
		{
			if (Application.Instance.SelectedItem is Items.GezelTextFile)
			{
        Forms.AddOpbSlaveIpBlockForm form =
          new Weazel.Toad.Application.Forms.AddOpbSlaveIpBlockForm();

        if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {

          Items.GezelTextFile file =
            Application.Instance.SelectedItem as Items.GezelTextFile;

          file.TextEditor.TextAreaControl.TextArea.InsertString(
            Format(
              form.IpBlockName,
              form.InterruptCount,
              32,
              form.RegisterCount,
              form.BaseAddress,
              form.HighAddress
            )
          );
        }
			}
		}

    void Instance_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
    {
      if (e.SelectedItem is Items.GezelTextFile)
      {
        Enabled = true;
      }
      else
      {
        Enabled = false;
      }
    }
  }
}
